package NewFeature;

import com.google.common.cache.Weigher;
import com.google.common.collect.Maps;
import org.junit.Test;
//import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Author MJS
 * @Date 2021/8/10 11:12
 * @Description
 */
public class Java8 {
    //Consumer:消费型接口
    @Test
    public void test1(){
        happy(100,m->{
            System.out.println("为了开心,消费了:"+m+"元");
        });
    }
    public void happy(double money,Consumer<Double>consumer){
        consumer.accept(money);
    }

    //Supplier：供给型接口
    @Test
    public void test2(){
        List<Integer> numList = getNumList(5, () -> (int) (Math.random() * 10));
        for (Integer integer : numList) {
            System.out.println(integer);
        }

    }
    //产生指定个数的整数，并放入集合中
    public List<Integer>getNumList(int num, Supplier<Integer>supplier){
        List<Integer>list=new ArrayList<>();

        for (int i = 0; i < num; i++) {
            Integer n = supplier.get();
            list.add(n);
        }

        return list;
    }
    //Function:函数式接口
    @Test
    public void test3(){
        String s = StrHandler("\t\t\t测试一下java8函数式接口", (str) -> str.trim());
        System.out.println(s);

        System.out.println(StrHandler("测试一下java8函数式接口", str -> str.substring(0, 3)));
    }
    public String StrHandler(String str, Function<String,String>function){
         return function.apply(str);
    }
    //Predicate:断言型接口
    @Test
    public void test4(){
        List<String> list = Arrays.asList("Hello","shang","lambda","sss","eatsdgd");

        List<String> filterStr = FilterStr(list, s-> s.length() >3);
        for(String str : filterStr) {
            System.out.println(str);
        }

    }
    //需求：将满足条件的字符串，放入到集合中
    public List<String>FilterStr(List<String>list, Predicate<String>predicate){
        List<String> strlist = new ArrayList<>();

        for(String str: list) {
            if (predicate.test(str)) {
                strlist.add(str);
            }
        }
        return strlist;

    }

    @Test
    public void test5(){
        Weigher<Integer, Integer> compare = Integer::compare;
        System.out.println(compare.weigh(3, 2));

        Maps.EntryTransformer<Integer, Object, Boolean> integerObjectBooleanEntryTransformer = Integer::equals;
        System.out.println(integerObjectBooleanEntryTransformer.transformEntry(1, 1));
    }

    @Test
    public void test6(){

    }

}
